package com.wn.class30.service.impl;

import com.wn.class30.entity.Cupon;
import com.wn.class30.entity.CuponExample;
import com.wn.class30.mapper.CuponDao;
import com.wn.class30.service.Couponservice;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CouponServiceImpl implements Couponservice {

    @Resource
    private CuponDao cuponDao;

    @Override
    public long count(Map<String, String> map) {
        return cuponDao.countByExample(getCuponExample(map));
    }

    @Override
    public List<Cupon> selectCoupons(Map<String, String> map) {
        return cuponDao.selectByExample(getCuponExample(map));
    }

    @Override
    public int insertSelective(Cupon record) {
        return cuponDao.insert(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer cuponId) {
        return cuponDao.deleteByPrimaryKey(cuponId);
    }

    @Override
    public int updateByPrimaryKeySelective(Cupon record) {
        return cuponDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public Cupon selectCuponById(Integer cuponId) throws Exception {
        return cuponDao.selectByPrimaryKey(cuponId);
    }

    public CuponExample getCuponExample(Map<String,String> map){
        CuponExample cuponExample = new CuponExample();
        CuponExample.Criteria criteria = cuponExample.createCriteria();
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String cuponId= map.get("cuponId");
        String cuponName= map.get("cuponName");
        String cuponRange= map.get("cuponRange");
        String cuponStatus= map.get("cuponStatus");
        if(!"".equals(cuponId) && cuponId != null){
            if(Integer.parseInt(cuponId)>0){
                criteria.andCuponIdEqualTo(Integer.parseInt(cuponId));
            }
        }
        if(!"".equals(cuponName) && cuponName !=null){
            criteria.andCuponNameLike("%"+cuponName+"%");
        }
        if(!"".equals(cuponRange) && cuponRange !=null){
            criteria.andCuponRangeLike("%"+cuponRange+"%");
        }
        if(!"".equals(cuponStatus) && cuponStatus != null){
            if(Integer.parseInt(cuponStatus)>0){
                criteria.andCuponStatusEqualTo(Integer.parseInt(cuponStatus));
            }
        }
        cuponExample.setLimit(limit);
        cuponExample.setOffset(page);

        return cuponExample;
    }
}
